package org.example.ski_demo;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class Main {
    public static void main(String[] args) {
        // 1. 创建代码生成器
        AutoGenerator autoGenerator = new AutoGenerator();

        // 2. 数据源配置 告诉生成器如何连接数据库
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("Mysql2025%");
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/ski_manage?useUnicode=true&characterEncoding=UTF-8");
        autoGenerator.setDataSource(dataSourceConfig);

        // 3. 全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOpen(false); // 生成后不自动打开目录
        globalConfig.setOutputDir(System.getProperty("user.dir") + "/src/main/java"); // 输出路径
        globalConfig.setAuthor("admin");
        globalConfig.setServiceName("%sService"); // 去掉默认的 "I" 前缀
        autoGenerator.setGlobalConfig(globalConfig);

        // 4. 包配置 根据数据库表结构自动生成 Java 代码
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("org.example.ski_demo");
        packageConfig.setEntity("entity");
        packageConfig.setMapper("mapper");
        packageConfig.setController("controller");
        packageConfig.setService("service");
        packageConfig.setServiceImpl("service.impl");
        autoGenerator.setPackageInfo(packageConfig);

        // 5. 策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setEntityLombokModel(true); // 启用 Lombok 注解
        strategyConfig.setNaming(NamingStrategy.underline_to_camel); // 数据库表名映射为驼峰命名
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel); // 字段命名策略
        strategyConfig.setInclude("admin", "coach", "equipment", "notice", "track", "user"); // 指定要生成的表
        autoGenerator.setStrategy(strategyConfig);

        // 6. 执行代码生成
        autoGenerator.execute();
    }
}
